<?php
                                                                                                                                                                                                                                                                                                                                        
// TTTTTTTTTTTTTTTTTTTTTTT                  MMMMMMMM               MMMMMMMM                                CCCCCCCCCCCCMMMMMMMM               MMMMMMMM  SSSSSSSSSSSSSSS 
// T:::::::::::::::::::::T                  M:::::::M             M:::::::M                             CCC::::::::::::M:::::::M             M:::::::MSS:::::::::::::::S
// T:::::::::::::::::::::T                  M::::::::M           M::::::::M                           CC:::::::::::::::M::::::::M           M::::::::S:::::SSSSSS::::::S
// T:::::TT:::::::TT:::::T                  M:::::::::M         M:::::::::M                          C:::::CCCCCCCC::::M:::::::::M         M:::::::::S:::::S     SSSSSSS
// TTTTTT  T:::::T  TTTTTppppp   ppppppppp  M::::::::::M       M::::::::::M   eeeeeeeeeeee          C:::::C       CCCCCM::::::::::M       M::::::::::S:::::S            
//         T:::::T       p::::ppp:::::::::p M:::::::::::M     M:::::::::::M ee::::::::::::ee       C:::::C             M:::::::::::M     M:::::::::::S:::::S            
//         T:::::T       p:::::::::::::::::pM:::::::M::::M   M::::M:::::::Me::::::eeeee:::::ee     C:::::C             M:::::::M::::M   M::::M:::::::MS::::SSSS         
//         T:::::T       pp::::::ppppp::::::M::::::M M::::M M::::M M::::::e::::::e     e:::::e     C:::::C             M::::::M M::::M M::::M M::::::M SS::::::SSSSS    
//         T:::::T        p:::::p     p:::::M::::::M  M::::M::::M  M::::::e:::::::eeeee::::::e     C:::::C             M::::::M  M::::M::::M  M::::::M   SSS::::::::SS  
//         T:::::T        p:::::p     p:::::M::::::M   M:::::::M   M::::::e:::::::::::::::::e      C:::::C             M::::::M   M:::::::M   M::::::M      SSSSSS::::S 
//         T:::::T        p:::::p     p:::::M::::::M    M:::::M    M::::::e::::::eeeeeeeeeee       C:::::C             M::::::M    M:::::M    M::::::M           S:::::S
//         T:::::T        p:::::p    p::::::M::::::M     MMMMM     M::::::e:::::::e                 C:::::C       CCCCCM::::::M     MMMMM     M::::::M           S:::::S
//       TT:::::::TT      p:::::ppppp:::::::M::::::M               M::::::e::::::::e                 C:::::CCCCCCCC::::M::::::M               M::::::SSSSSSS     S:::::S
//       T:::::::::T      p::::::::::::::::pM::::::M               M::::::Me::::::::eeeeeeee          CC:::::::::::::::M::::::M               M::::::S::::::SSSSSS:::::S
//       T:::::::::T      p::::::::::::::pp M::::::M               M::::::M ee:::::::::::::e            CCC::::::::::::M::::::M               M::::::S:::::::::::::::SS 
//       TTTTTTTTTTT      p::::::pppppppp   MMMMMMMM               MMMMMMMM   eeeeeeeeeeeeee               CCCCCCCCCCCCMMMMMMMM               MMMMMMMMSSSSSSSSSSSSSSS   
//                        p:::::p                                                                                                                                       
//                        p:::::p                                                                                                                                       
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       ppppppppp                                                                                                                                      
                                                                                                                                                                     
//  _____      __  __         ____ __  __ ____  
// |_   __ __ |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
//   | || '_ \| |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
//   | || |_) | |  | |  __/ | |___| |  | |___) |    | WECHAT: he4966
//   |_|| .__/|_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2021/08/25
//      |_|                                         | TpMeCMS

namespace app\api\controller;
use app\api\controller\Tpmecms;

use think\Db;
use think\Config;

/**
 * 提现💰
 * 
 */
class Tixian extends Tpmecms{

    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['TiXianUser','wechat_tixian'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];
    /**
     * 提现给用户
     *
     * @param integer $uid 用户id
     * @param float $money 钱
     * @param string $ps 备注
     * @return void
     */
    protected function TiXianUser($user,$money=0.01,$order,$ps='提现')
    {
        // $user = Db::name('user')->where('id',$uid)->find();
        if($user){
            
            // $this->ExchangeToWechatBalance($openid,$money,$out_order,$desc='提现说明',$bug=false)
            // if($money<1){
            //     if($money>0){
            //         $money = $money *100;
            //     }else{
            //         $this->error('金额不正确',$money);
            //     }
            // }

            //提现到微信
            $res = $this->ExchangeToWechatBalance($user['openid'],$money*100,$order,$ps);//,true
            
            
            //提现成功减去余额
            if($res){
                $this->DecUserMoney($uid,$money,$ps);
                // $this->success('提现成功');
            }


        }
    }


    /**
     * 微信提现💰
     * 
     * @ApiInternal()
     * @param int $id 提现id
     * @return void
     */
    public function wechat_tixian($id)
    {


        $out = Db::name('out')->where('id',$id)->find();
        if(strlen($out['order'])>10){
            $order = $out['order'];
        }else{
            $order = $this->rand_order('TiXian',$out['uid']);
            Db::name('out')->where('id',$id)->update(['order'=>$order]);
        }
        $user = Db::name('user')->where('id',$out['uid'])->find();



        if($user['money']>=$out['money']){
            $this->TiXianUser($user,$out['money']*100,$order,'提现');
        }else{
            $this->error('余额不足',$user['money']);
        }
    }


    /**
     * 添加银行提现
     *
     * 
     * @param string $bank_number 银行卡号
     * @param string $name 姓名
     * @param string $bank_name 开户行
     * @param string $money 金额
     * @return void
     */
    public function add_bank_tixian($pay_status = '2',$bank_number=null ,$name=null ,$bank_name=null ,$money)
    {

        //@param string $pay_status 不用传，方式:1=微信,2=银行转账
        //方式:1=微信,2=银行转账
        $data['uid'] = $this->auth->id;
        $data['time'] = time();
        $data['status'] = '1';
        switch ($pay_status) {
            case '1':
                $data['money'] = $money;
                break;

            case '2':
                $data['money'] = $money;
                $data['bank_number'] = $bank_number;
                $data['pay_status'] = $pay_status;
                $data['name'] = $name;
                $data['bank_name'] = $bank_name;
                break;
            
            default:
                $data['money'] = $money;
                $data['bank_number'] = $bank_number;
                $data['pay_status'] = $pay_status;
                $data['name'] = $name;
                $data['bank_name'] = $bank_name;
                break;
        }

        $res = Db::name('out')->insert($data);
        if($res){
            $this->success('申请成功');
        }


    }


    /**
     * 获取钱到日志列表💰
     * 
     * @param integer $page 页1
     * @param integer $limit 数量10
     * @ApiSummary  (money变更余额_before变更前余额_after变更后余额)
     * @return void
     */
    public function get_money_log_list($page=1,$limit=10)
    {
        $list = Db::name('user_money_log')
            ->where('user_id',$this->auth->id)
            ->order('createtime desc')
            ->page($page,$limit)
            ->select();

        $this->success('ok',$list);
    }



    // public function find_stores_tx($txorder = 'TS2020101498514910')
    // {

    //     $app = $this->get_pay_app();
    //     $res_tx = $app->transfer->queryBalanceOrder($txorder);

        
    //     if($res_tx['return_code']=='SUCCESS' && $res_tx['result_code']=='SUCCESS' &&$res_tx['status']=='SUCCESS'){//判断如果支付成功

    //         Db::name('shop_tx_log_stores')->where('txorder',$txorder)->update(['end_time'=>strtotime($res_tx['payment_time']),'status'=>'1']);
    //     }
    //     halt($res_tx);
    // }

}